package com.grace.controller;


import com.grace.entity.Product;
import com.grace.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 查找所有商品
     */
    @RequestMapping("/findAll")
    @Secured("ROLE_ADMIN")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<Product> list = productService.findAll();
        mv.addObject("productList", list);
        mv.setViewName("product-list2");
        return mv;
    }

    /**
     * 新增商品
     */
    @RequestMapping("/save")
    public String save(Product product) {

        productService.save(product);
        return "redirect:findAll";
    }

}
